import 'dart:math';

// 转为数字
num? toNumber(Object? value) {
  if (value is num) return value;
  if (value is String) {
    num? result = num.tryParse(value);
    return (result != null && !result.isNaN) ? result : null;
  }
  return null;
}

// 转为整数（四舍五入）
int? toInt(Object? value) {
  if (value is num) return value.round();
  if (value is String) {
    num? result = num.tryParse(value);
    return (result != null && !result.isNaN) ? result.round() : null;
  }
  return null;
}

// 数字精度转换（四舍五入）
num toFixed(num value, [digit = 2]) {
  String numStr = value.toStringAsFixed(digit);
  return double.parse(numStr);
}

// 获取小数点长度
int _digitLength(num value) {
  List expSplit = value.toString().split(RegExp(r'[eE]'));
  int expValue = expSplit.length > 1 ? int.parse('${expSplit[1]}') : 0;
  if (expSplit[0].contains('.')) {
    return expSplit[0].split('.')[1].length - expValue;
  }
  return 0;
}

// 强制去除浮点数
int _floatFixed(num value) {
  if (value.toString().contains(RegExp(r'[eE]'))) {
    int dLen = _digitLength(value);
    return (dLen > 0 ? value * pow(10, dLen) : value).toInt();
  }
  return int.parse(value.toString().replaceAll('.', ''));
}

// 精确计算-加法
num calcAdd(num num1, num num2) {
  num accuracy = pow(10, max(_digitLength(num1), _digitLength(num2)));
  return (calcMultiply(num1, accuracy) + calcMultiply(num2, accuracy)) / accuracy;
}

// 精确计算-减法
num calcSubtract(num num1, num num2) {
  num accuracy = pow(10, max(_digitLength(num1), _digitLength(num2)));
  return (calcMultiply(num1, accuracy) - calcMultiply(num2, accuracy)) / accuracy;
}

// 精确计算-乘法
num calcMultiply(num num1, num num2) {
  int accuracy = _digitLength(num1) + _digitLength(num2);
  int leftValue = _floatFixed(num1) * _floatFixed(num2);
  return leftValue / pow(10, accuracy);
}

// 精确计算-除法
num calcDivide(num num1, num num2) {
  int num1Changed = _floatFixed(num1);
  int num2Changed = _floatFixed(num2);
  return calcMultiply(
    num1Changed / num2Changed,
    pow(10, _digitLength(num2) - _digitLength(num1)),
  );
}

// 米(m)转英尺(ft)
num meterToFt(num value, [int? digit]) {
  double ratio = 3.280839895;
  int digitLen = _digitLength(value);
  return toFixed(value * ratio, digit ?? digitLen);
}

// 平方米(m²)转平方英尺(ft²)
num meter2ToFt2(num value, [int? digit]) {
  double ratio = 10.7639104167;
  int digitLen = _digitLength(value);
  return toFixed(value * ratio, digit ?? digitLen);
}

// 英尺(ft)转米(m)
num ftToMeter(num value, [int? digit]) {
  double ratio = 3.280839895;
  int digitLen = _digitLength(value);
  return toFixed(value / ratio, digit ?? digitLen);
}

// 平方英尺(ft²)转平方米(m²)
num ft2ToMeter2(num value, [int? digit]) {
  double ratio = 10.7639104167;
  int digitLen = _digitLength(value);
  return toFixed(value / ratio, digit ?? digitLen);
}

// 摄氏度(°C)转华氏度(°F)
num celsiusToFahrenheit(num value, [int? digit]) {
  int digitLen = _digitLength(value);
  return toFixed(value * 9 / 5 + 32, digit ?? digitLen);
}

// 华氏度(°F)转摄氏度(°C)
num fahrenheitToCelsius(num value, [int? digit]) {
  int digitLen = _digitLength(value);
  return toFixed((value - 32) * 5 / 9, digit ?? digitLen);
}
